import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);

export class DescriptionHelper {
    // 生成卫星属性信息
    static renderDescription(time, name, position, passes, isGroundStation, tle) {
        let description = `
      <div class="ib">
        <h3>位置</h3>
        <table class="ibt">
          <thead>
            <tr>
              <th>名称</th>
              <th>经度</th>
              <th>纬度</th>
              <th>${isGroundStation ? '高程' : '高度'}</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>${name}</td>
              <td>${position.longitude.toFixed(2)}&deg</td>
              <td>${position.latitude.toFixed(2)}&deg</td>
              <td>${(position.height / 1000).toFixed(2)} km</td>
            </tr>
          </tbody>
        </table>
        ${this.renderPasses(passes, time, isGroundStation)}
        ${typeof tle === 'undefined' ? '' : this.renderTLE(tle)}
      </div>
    `;
        return description;
    }

    // 生成卫星过境信息
    static renderPasses(passes, time, showPassName) {
        if (passes.length === 0) {
            const html = `
        <h3>过境</h3>
        <div class="ib-text">没有设置监测点</div>
        `;
            return html;
        }

        const start = dayjs(time);
        const upcomingPassIdx = passes.findIndex(pass => {
            return dayjs(pass.end).isAfter(start);
        });
        if (upcomingPassIdx < 0) {
            return '';
        }
        const upcomingPasses = passes.slice(upcomingPassIdx);

        const htmlName = showPassName ? '<th>Name</th>\n' : '';
        const html = `
      <h3>过境</h3>
      <table class="ibt">
        <thead>
          <tr>
            ${htmlName}
            <th>倒计时</th>
            <th>开始时间</th>
            <th>结束时间</th>
            <th>El</th>
            <th>Az</th>
          </tr>
        </thead>
        <tbody>
          ${upcomingPasses.map(pass => this.renderPass(start, pass, showPassName)).join('')}
        </tbody>
      </table>
    `;
        return html;
    }
    // 生成卫星过境信息
    static renderPass(time, pass, showPassName) {
        function pad2(num) {
            return String(num).padStart(2, '0');
        }
        let countdown = 'ONGOING';
        if (dayjs(pass.end).diff(time) < 0) {
            countdown = 'PREVIOUS';
        } else if (dayjs(pass.start).diff(time) > 0) {
            countdown = `${pad2(dayjs(pass.start).diff(time, 'days'))}:${pad2(
                dayjs(pass.start).diff(time, 'hours') % 24
            )}:${pad2(dayjs(pass.start).diff(time, 'minutes') % 60)}:${pad2(
                dayjs(pass.start).diff(time, 'seconds') % 60
            )}`;
        }
        const htmlName = showPassName ? `<td>${pass.name}</td>\n` : '';
        const html = `
      <tr>
        ${htmlName}
        <td>${countdown}</td>
        <td>${dayjs(pass.start).format('DD.MM HH:mm:ss')}</td>
        <td>${dayjs(pass.end).format('HH:mm:ss')}</td>
        <td class="ibt-right">${pass.pitch.toFixed(0)}&deg</td>
        <td class="ibt-right">${pass.sideSwingAngle.toFixed(2)}&deg</td>
      </tr>
    `;
        return html;
    }

    // 生成卫星轨道两根数信息
    static renderTLE(tle) {
        const html = `
      <h3>TLE 参数</h3>
      <div class="ib-code"><code>${tle.slice(1, 3).join('\n')}</code></div>`;
        return html;
    }
}
